module game {
	class AdventureHandle {
		public constructor() {
		}
		
        public handle(latticeId: number, scene:view.MapView): any{
            var latticeConfig = totalConfig[latticeId]
            var info = {
                type: 0, //0普通事件,1随机事件,2任务时间
                content: '',
                latticeId: latticeId
            }

            if(latticeConfig.taskID){ //任务
                var state = game.playerHandle.taskStateByLattice(latticeConfig.taskID,latticeId)
                if(state === 2){
                    info.type = 2
                    info['taskId'] = latticeConfig.taskID
                    var taskConfig = totalConfig[latticeConfig.taskID]
                    var latticeInfo:any
                    for(var i in taskConfig.taskContent) {
                        if(taskConfig.taskContent[i].latticeId === latticeId) {
                            latticeInfo = taskConfig.taskContent[i]
                            break
                        }
                    }
                    console.log(latticeInfo)
                    info['latticeInfo'] = latticeInfo
                    return info
                }
            }
            
            //采集, 采矿, 钓鱼, 遇怪, 伤害地形, 恢复地形, 获得buff地形, 副本, 事件, 使用物品地形
            if(latticeConfig.eventsTrigger && Math.floor(Math.random() * 100) < latticeConfig.eventsTrigger){
                var eventId = game.utilsHandle.getRandomOne(latticeConfig.events)
                var eventConfig = totalConfig[eventId] 
                info.type = 1
                info['eventId'] = eventId
                return info
            }
            switch(latticeConfig.latticeType){
                case 1://采集
                    info.content= this.collection(latticeConfig)
                    break;
                case 2://采矿
                    info.content = this.mining(latticeConfig)
                    break;
                case 3://钓鱼
                    info.content = this.fishing(latticeConfig)
                    break;
                case 4://遇怪
                    info.content = this.needMonster(latticeConfig,scene)
                    break;
                case 5://伤害地形
                    info.content = this.injure(latticeConfig)
                    break;
                case 6://恢复地形
                    info.content = this.recover(latticeConfig)
                    break;
                case 7://获得buff地形
                    info.content = this.getBuff(latticeConfig)
                    break;
                case 8://副本
                    info.content = this.instance(latticeConfig,scene)
                    break;
                case 9://事件
                    info.content = this.needEvent(latticeConfig)
                    break;
                case 10://使用物品地形
                    info.content = this.useItem(latticeConfig)
                    break;     
                case 11://荒地
                    info.content = this.other(latticeConfig)
                    break; 
            }
            return info 
        }

        //采集
        private collection(latticeConfig):string{
            var itemId = latticeConfig.itemId
            var item = new game.ItemComponent({
                id: itemId,
                stack:1
            })
            gplayer.bag.addItem(item)
            var contents = latticeConfig.content
            var content = utilsHandle.getRandomOne(contents)
            return content + '\n采集到了' + templateName[itemId] + ' X 1'
        }
        
        //采矿
        private mining(latticeConfig): string {
            var itemId = latticeConfig.itemId
            var item = new game.ItemComponent({
                id: itemId,
                stack: 1
            })
            gplayer.bag.addItem(item)
            var contents = latticeConfig.content
            var content = utilsHandle.getRandomOne(contents)
            return content + '\n挖到了' + templateName[itemId]+ ' X 1'
        }
        
        //钓鱼
        private fishing(latticeConfig): string {
            var itemId = latticeConfig.itemId
            var item = new game.ItemComponent({
                id: itemId,
                stack: 1
            })
            gplayer.bag.addItem(item)
            var contents = latticeConfig.content
            var content = utilsHandle.getRandomOne(contents)
            return content + '\n钓了半天, 钓到了' + templateName[itemId]+ ' X 1'
        }
        
        //遇怪
        private needMonster(latticeConfig,scene: view.MapView): string {
            var monsterId = latticeConfig.monsterId           
            var battleView = new view.BattleView2(monsterId)
            scene.adventureTimer.stop()
            scene.addChild(battleView)  
            var contents = latticeConfig.content
            var content = utilsHandle.getRandomOne(contents)
            return content
        }
        //伤害地形
        private injure(latticeConfig): string {
            var contents = latticeConfig.content
            var content = utilsHandle.getRandomOne(contents)
            return content + '\n受到了' + latticeConfig.value + '伤害'
        }
        //恢复地形
        private recover(latticeConfig): string {
            var contents = latticeConfig.content
            var content = utilsHandle.getRandomOne(contents)
            return content + '\n恢复了' + latticeConfig.value + '生命'
        }
        //获得buff地形
        private getBuff(latticeConfig): string {
            var contents = latticeConfig.content
            var content = utilsHandle.getRandomOne(contents)
            return content + '\n获得了+20%的金钱掉率buff'
        }
        //副本 
        private instance(latticeConfig,scene: view.MapView): string {
            scene.adventureTimer.stop()
            scene.popLayer(latticeConfig)
            var contents = latticeConfig.content
            var content = utilsHandle.getRandomOne(contents)
            return content
        }
        //事件
        private needEvent(latticeConfig): string {
            var contents = latticeConfig.content
            var content = utilsHandle.getRandomOne(contents)
            return content
        }
        //使用物品地形
        private useItem(latticeConfig):string {
            var itemId = latticeConfig.itemId
            var item = new game.ItemComponent({
                id: itemId,
                stack: 1
            })
            gplayer.bag.addItem(item)
            var contents = latticeConfig.content
            var content = utilsHandle.getRandomOne(contents)
            return content + '\n获得了' + templateName[itemId] + ' X 1'
        }
        //使用物品地形
        private other(latticeConfig): string {
            var contents = latticeConfig.content
            var content = utilsHandle.getRandomOne(contents)
            return content + '\n什么事情也没发生'
        }
	}
    export var adventureHandle = new AdventureHandle()
}
